package com.svt;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class ShellUtil {
//    public static Map executeShell(String shellCommand){
//    	Map result = new HashMap();
//    	StringBuffer std = new StringBuffer();
//    	StringBuffer err = new StringBuffer();
//    	try {
//    		String osName = System.getProperty("os.name").toLowerCase();
//    		
//    		String[] cmd = osName.indexOf("windows")>-1?(new String[]{"cmd", "/c", shellCommand}):new String[]{"/bin/sh", "-c", shellCommand};
//    	    Process p = Runtime.getRuntime().exec(cmd);  
//    	    BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(p.getInputStream()));  
//    	    String line;  
//    	    while ((line=bufferedReader.readLine()) != null) {  
//    	        std.append(line).append("\n");
//    	        System.out.println("std----------"+line);
//    	    }
//    	    BufferedReader brError = new BufferedReader(new InputStreamReader(p.getErrorStream()));  
//    	    String errline;  
//    	    while ((errline = brError.readLine()) != null) {  
//    	    	err.append(errline).append("\n"); 
//    	    }  
//    	    System.out.println("wait begin");
//    	    int c=p.waitFor();  
//    	    System.out.println("wait end");
//    	} catch (Exception e) {
//    		result.put("success", "0");
//    	}
//    	if(err.length()>0){
//    		result.put("success", "0");
//    		result.put("msg", err.toString());
//    	}else{
//    		result.put("success", "1");
//    		result.put("msg", std.toString());
//    	}
//    	return result;
//    }
    
	public static StringBuffer std = new StringBuffer();
	
	public static StringBuffer err = new StringBuffer();
    public static Map executeShell(String shellCommand){
    	Map result = new HashMap();
    	StringBuffer std = new StringBuffer();
    	StringBuffer err = new StringBuffer();
    	try {
    		String osName = System.getProperty("os.name").toLowerCase();
    		
    		String[] cmd = osName.indexOf("windows")>-1?(new String[]{"cmd", "/c",shellCommand}):new String[]{"/bin/sh", "-c", shellCommand};
    	    
    		Process process = Runtime.getRuntime().exec(cmd); 
    		
    		Thread t1 = new PrintStream(process.getInputStream(),std);
    		Thread t2 = new PrintStream(process.getErrorStream(),err);
    		t1.start();
    		t2.start();
    	    
    	    System.out.println("wait begin");
    	    process.waitFor();
    	    System.out.println("wait end");
    	} catch (Exception e) {
    		e.printStackTrace();
    		result.put("success", "0");
    	}
    	if(err.length()>0){
    		result.put("success", "0");
    		result.put("msg", err.toString());
    	}else{
    		result.put("success", "1");
    		result.put("msg", std.toString());
    	}
    	return result;
    }
 
    public static void main(String[] args) {
    	System.out.println(executeShell("ping 127.0.0.1 -n 5"));
    }
    
    
}

class PrintStream extends Thread 
{
    InputStream is = null;
    StringBuffer rst = null;
    
    public PrintStream(InputStream is,StringBuffer rst) 
    {
    	this.is = is;
    	this.rst = rst;
    } 

    public void run() 
    {
        try 
        {
//            while(this != null) 
//            {
//                int _ch = is.read();
//                if(_ch != -1) {
//                    rst.append((char)_ch);
//                    System.out.println((char)_ch);
//                }    
//                else break;
//            }
        	BufferedReader br = new BufferedReader(new InputStreamReader(is));
        	String line;  
    	    while ((line = br.readLine()) != null) {  
    	    	rst.append(line).append("\n");
    	    }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        } 
    }
}